<style>
    i{font-size:15px;color:#e0e0e0;margin-left:10px;cursor:pointer;
        border:1px solid #fff;padding:2px;}
    i:hover{color:#1E9FFF;}
    i2{font-size:15px;color:#e0e0e0;margin-left:10px;cursor:pointer;
        border:1px solid #fff;padding:2px;}
    .disabled{background:#e6e6e6;}
    label{width:100px!important;}
    .layui-input-block{margin-left:150px;}
    .auth{color:#84D945;}
</style>

<div class="layui-form">

    <div class="layui-form-item layui-col-md6">
        <label class="layui-form-label">URL验证<i2 class="layui-icon is_auth1 auth">&#xe672;</i2></label>
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-sm layui-btn-disabled is_auth2" style="margin-top:5px;">已验证</button>
        </div>
    </div>

    <div class="layui-form-item layui-col-md6">
        <label class="layui-form-label">开发者ID<i class="layui-icon">&#xe642;</i></label>
        <div class="layui-input-block">
            <input type="text" name="client_id" autocomplete="off" class="layui-input disabled" disabled>
        </div>
    </div>

    <div class="layui-form-item layui-col-md6">
        <label class="layui-form-label">开发者密码<i class="layui-icon">&#xe642;</i></label>
        <div class="layui-input-block">
            <input type="text" name="client_secret" autocomplete="off" class="layui-input disabled" disabled>
        </div>
    </div>

    <div class="layui-form-item layui-col-md6">
        <label class="layui-form-label">Token<i class="layui-icon">&#xe642;</i></label>
        <div class="layui-input-block">
            <input type="text" name="token" autocomplete="off" class="layui-input disabled" disabled>
        </div>
    </div>

    <div class="layui-form-item layui-col-md6">
        <label class="layui-form-label">AESKey<i class="layui-icon">&#xe642;</i></label>
        <div class="layui-input-block">
            <input type="text" name="encode_key" autocomplete="off" class="layui-input disabled" disabled>
        </div>
    </div>

</div>

<script>
    var configs;
    $(function(){
        layui.use('form');
        //获取config参数列表
        GetConifgList();
    });

    //获取config参数列表
    function GetConifgList(){
        var need = 'client_id,client_secret,auth,token,encode_key';
        dink.post('/handler/weapp/getConfigList',{need:need},function(res){
            if(res.ret == 1){
                configs = res.data;
                //设置验证状态
                SetAuth();
                $.each(res.data,function(i,ele){
                    $('input[name='+i+']').val(ele);
                });
            }else{
                layer.msg(res.msg);return;
            }
        });
    }

    //编辑按钮事件
    dink.click('i',function(){
        $(this).parent().next().children().attr('disabled',false);
        $(this).parent().next().children().removeClass('disabled');
        $(this).parent().next().children().focus();
    });

    //编辑完事件
    $('.layui-input').blur(function(){
        var that = $(this);
        if(configs[that.attr('name')] !== that.val()){
            layer.confirm('确定提交修改？',{title:'Tips'},function(){
                var title = that.attr('name');
                var content = that.val();
                dink.post('/handler/weapp/editConfig',{title:title,content:content},function(res){
                    layer.msg(res.msg);
                    if(res.ret == 1){
                        LockInput(that);
                    }
                });
            },function(){
                LockInput(that);
            });
        }else{
            LockInput(that);
        }
    });

    //锁定input并重新获取数据
    function LockInput(e){
        e.attr('disabled',true);
        e.addClass('disabled');
        GetConifgList();
    }

    //设置验证状态
    function SetAuth(){
        if(configs.auth == 1){
            $('.is_auth1').addClass('auth');
            $('.is_auth2').removeClass('layui-btn-normal');
            $('.is_auth2').addClass('layui-btn-disabled');
            $('.is_auth2').text('已验证');
        }else{
            $('.is_auth1').removeClass('auth');
            $('.is_auth2').removeClass('layui-btn-disabled');
            $('.is_auth2').addClass('layui-btn-normal');
            $('.is_auth2').text('点击验证');
        }
    }

    //url验证事件
    dink.click('.is_auth2',function(){
        if($(this).text() == '点击验证'){
            dink.post('/handler/weapp/test',{},function(res){
                // dink.post('/handler/weapp/auth',{},function(res){
                layer.msg(res.msg);
                // GetConifgList();
            });
        }
    });
</script>